package com.jiang.doc.service;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ReportingEngine;
import com.aspose.words.SaveFormat;
import com.jiang.doc.entity.BaseInfo;
import com.jiang.doc.entity.Resume;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class DocService {


    /**
     * 导出简历
     *
     * @param response r
     */
    public void export(HttpServletResponse response) {
        Document document = this.getDocument();
        this.output(response, document);
    }

    /**
     * 输出文档
     *
     * @param response res
     * @param doc      doc
     */
    private void output(HttpServletResponse response, Document doc) {
        //生成导出成绩单文件名称
        String nowTime = String.valueOf(System.currentTimeMillis());
        try (ServletOutputStream ops = response.getOutputStream()) {
            String fileName = URLEncoder.encode("简历" + nowTime + ".pdf", "UTF-8");
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setContentType("application/octet-stream");
            doc.save(ops, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("输出简历失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取doc
     *
     * @return doc
     */
    private Document getDocument() {
        // 获取资源doc路径下的简历template.doc模板
        final ClassPathResource classPathResource = new ClassPathResource("doc\\template.docx");
        // 组装数据
        final Document doc;
        try (InputStream ins = classPathResource.getInputStream()) {
            //将输入流转为doc
            doc = new Document(ins);
            //处理标签信息
            this.getTag(doc);
            //获取简历信息
            Resume resume = this.getResume();
            doc.updateFields();
            final ReportingEngine engine = new ReportingEngine();
            engine.buildReport(doc, resume, "r");
            return doc;
        } catch (Exception e) {
            log.error("生成简历失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 处理特殊字符相关操作
     * @param doc doc
     * @throws Exception 异常
     */
    private void getTag(Document doc) throws Exception{
        // doc构建
        DocumentBuilder builder = new DocumentBuilder(doc);

        // 定位头像书签位置
        builder.moveToBookmark("avatar");
        // 插入图片
        builder.insertImage("https://gitee.com/jiang5sx/code_world/raw/master/images/avatar.jpg",45,45);

        //定位能否出差书签位置
        builder.moveToBookmark("travel0");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        // 设置字符大小
        builder.getFont().setSize(12);
        // 对号字符
        builder.write("\uF0FE");

        // 定位能否出差书签位置
        builder.moveToBookmark("travel1");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        builder.getFont().setSize(16);
        builder.write("□");

        //定位能否加班书签位置
        builder.moveToBookmark("overtime0");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        // 设置字符大小
        builder.getFont().setSize(12);
        // 对号字符
        builder.write("\uF0FE");

        // 定位能否加班书签位置
        builder.moveToBookmark("overtime1");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        builder.getFont().setSize(16);
        builder.write("□");

        //定位能否接收调岗书签位置
        builder.moveToBookmark("transfers0");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        // 设置字符大小
        builder.getFont().setSize(12);
        // 对号字符
        builder.write("\uF0FE");

        // 定位能否接收调岗书签位置
        builder.moveToBookmark("transfers1");
        // 设置字符名称
        builder.getFont().setName("Wingdings");
        builder.getFont().setSize(16);
        builder.write("□");
    }

    /**
     * 组装简历信息
     *
     * @return r
     */
    private Resume getResume() {
        Resume resume = new Resume();
        this.getBaserInfo(resume);
        return resume;
    }

    /**
     * 组装个人资料信息
     *
     * @param resume r
     */
    private void getBaserInfo(Resume resume) {
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setName("小明")
                .setSex("男")
                .setEthnic("汉族")
                .setUrl("xxxx")
                .setPolitics("群众");
        resume.setBaseInfo(baseInfo);
    }

    public static void main(String[] args) {
        List<String> a = Arrays.asList("IF","CIF","IIF","I-IF","I IF","OL","LRN","CLRN","OL","试飞");
        for (String s : a) {
            if (!s.contains("IF")){
                System.out.println("不存在-->"+s);
            }
        }

        Long x = 125L;
        int v = 293;

        float i = (x / (float)v);
        int round = Math.round(i * 100);
        System.out.println(round);

    }

}
